/**
 * 创元汇 - WebName
 * https://menger.me
 * @大梦
 */

import React, {Component} from 'react'
import {
    Text,
    View,
    Image,
    TextInput,
    ScrollView,
    StyleSheet,
    TouchableOpacity,
    StatusBar
} from 'react-native'
import NetRequest from '../../utils/utilsRequest'
import ServicesApi from '../../constants/ServicesApi'
import GlobalStyles from '../../constants/GlobalStyles'
import NavigationBar from '../../components/common/NavigationBar'
import UtilsView from '../../utils/utilsView'
import { toastShort, toastLong, consoleLog } from '../../utils/utilsToast'

export default class WebName extends Component {

    constructor(props) {
        super(props);
        const { params } = this.props.navigation.state;
        this.state =  {
            id: params.zichan_id,
            target: '',
            status: '',
            custodian: '',
            style: '',
            principal: '',
            redemption_principal: '',
            profit: '',
            profit_all: '',
        }
        // this.netRequest = new NetRequest();
    }

    componentDidMount(){
        this.willFocus = this.props.navigation.addListener('willFocus', (payload) => {
            this.loadNetData();
        })
    }

    componentWillUnmount(){
        this.willFocus.remove();
        this.onBack();
    }

    onBack = () => {
        this.props.navigation.state.params.onCallBack();
        this.props.navigation.goBack();
    }

    updateState= (state) => {
        if (!this) { return };
        this.setState(state);
    }

    loadNetData = () => {
        let url = ServicesApi.Myassetdetail;
        let data = {
            id: this.state.id,
            member_id: global.user.userData.id,
            token: global.user.userData.token
        }

        Services.post(url, data)
            .then( result => {
                if (result && result.code == StatusCode.SUCCESS_CODE) {
                    console.log(result.data);
                    this.updateState({
                        target: result.data.target,
                        status: result.data.status,
                        custodian: result.data.custodian,
                        style: result.data.style,
                        principal: result.data.principal,
                        redemption_principal: result.data.redemption_principal,
                        profit: result.data.profit,
                        profit_all: result.data.profit_all,
                    })
                }else{
                    toastLong(result.msg);
                }
            })
            .catch( error => {
                toastShort('服务器请求失败，请稍后重试！');
            })
    }

    Subziliao = () => {
        let url = ServicesApi.Myassetedit;
        let data = {
            id: this.state.id,
            custodian: this.state.custodian,
            target: this.state.target,
            style: this.state.style,
            principal: this.state.principal,
            redemption_principal: this.state.redemption_principal,
            profit: this.state.profit,
            profit_all: this.state.profit_all,
            member_id: global.user.userData.id,
            token: global.user.userData.token,
        }
        Services.post(url, data, true)
        .then( result => {
            console.log(result);
            if (result && result.code == StatusCode.SUCCESS_CODE) {
                toastShort(result.msg);
                this.onBack();
            }else{
                toastLong(result.msg);
            }
        })
        .catch( error => {
            // toastShort('服务器请求失败，请稍后重试！');
        })
    }

    render(){
        let navigationBar = {
            backgroundColor: '#fff',
            borderBottomColor: '#f2f2f2',
            borderBottomWidth: 1
        }
        return (
            <View style={styles.container}>
                <NavigationBar
                    title = {this.state.target}
                    style = {navigationBar}
                    titleStyle = {{color: '#333333'}}
                    leftButton = {UtilsView.getLeftBlackButton(() => this.onBack())}
                    rightButton = {UtilsView.getRightKefuBlackButton(() => this.onPushNavigator('Kefu'))}
                />
                <StatusBar
                    animated = {true}
                    hidden = {false}
                    backgroundColor = {'transparent'}
                    translucent = {true}
                    barStyle = {'dark-content'}
                />
                <ScrollView keyboardShouldPersistTaps={'handled'}>
                    <View style={[GlobalStyles.whiteModule, {marginTop: 10}]}>
                        <View style={GlobalStyles.userlist}>
                            <View style={GlobalStyles.userlistright}>
                                <Text style={GlobalStyles.userlisttext}>投资人姓名</Text>
                                <View style={GlobalStyles.flexRowEnd}>
                                    <Text >{global.invest.investInfo.truename}</Text>
                                </View>                                    
                            </View>
                        </View> 
                        <View style={GlobalStyles.userlist}>
                            <View style={GlobalStyles.userlistright}>
                                <Text style={GlobalStyles.userlisttext}>份额管理人</Text>
                                <View style={GlobalStyles.flexRowEnd}>
                                    <TextInput
                                        placeholder={'请填写份额管理人'}
                                        value={this.state.custodian}
                                        onChangeText={(text) => {
                                            this.setState({
                                                custodian: text
                                            })
                                        }}
                                        style={[GlobalStyles.cellInput, GlobalStyles.isIOS ? null : styles.inputAndroid, {textAlign: 'right', color: '#bbbbc1', fontSize: 14, width: 210, }]} 
                                        underlineColorAndroid={'transparent'}
                                    >
                                    </TextInput>
                                </View>                                    
                            </View>
                        </View> 
                        <View style={GlobalStyles.userlist}>
                            <View style={GlobalStyles.userlistright}>
                                <Text style={GlobalStyles.userlisttext}>投资标的</Text>
                                <View style={GlobalStyles.flexRowEnd}>
                                    <TextInput
                                        placeholder={'请填写投资标的'}
                                        value={this.state.target}
                                        onChangeText={(text) => {
                                            this.setState({
                                                target: text
                                            })
                                        }}
                                        style={[GlobalStyles.cellInput, GlobalStyles.isIOS ? null : styles.inputAndroid, {textAlign: 'right', color: '#bbbbc1', fontSize: 14, width: 210, }]} 
                                        underlineColorAndroid={'transparent'}
                                    >
                                    </TextInput>
                                </View>                                    
                            </View>
                        </View> 
                        <View style={GlobalStyles.userlist}>
                            <View style={GlobalStyles.userlistright}>
                                <Text style={GlobalStyles.userlisttext}>投资方式</Text>
                                <View style={GlobalStyles.flexRowEnd}>
                                    <TextInput
                                        placeholder={'请填写投资方式'}
                                        value={this.state.style}
                                        onChangeText={(text) => {
                                            this.setState({
                                                style: text
                                            })
                                        }}
                                        style={[GlobalStyles.cellInput, GlobalStyles.isIOS ? null : styles.inputAndroid, {textAlign: 'right', color: '#bbbbc1', fontSize: 14, width: 210, }]} 
                                        underlineColorAndroid={'transparent'}
                                    >
                                    </TextInput>
                                </View>                                    
                            </View>
                        </View> 
                        <View style={GlobalStyles.userlist}>
                            <View style={GlobalStyles.userlistright}>
                                <Text style={GlobalStyles.userlisttext}>投资本金</Text>
                                <View style={GlobalStyles.flexRowEnd}>
                                    <TextInput
                                        placeholder={'请填写投资本金'}
                                        value={this.state.principal}
                                        onChangeText={(text) => {
                                            this.setState({
                                                principal: text
                                            })
                                        }}
                                        style={[GlobalStyles.cellInput, GlobalStyles.isIOS ? null : styles.inputAndroid, {textAlign: 'right', color: '#bbbbc1', fontSize: 14, width: 210, }]} 
                                        underlineColorAndroid={'transparent'}
                                        keyboardType="numeric"
                                    >
                                    </TextInput>
                                    <Text style={GlobalStyles.userlistrightDanwei}>元</Text>
                                </View>                                    
                            </View>
                        </View> 
                        <View style={GlobalStyles.userlist}>
                            <View style={GlobalStyles.userlistright}>
                                <Text style={GlobalStyles.userlisttext}>已赎回投资本金</Text>
                                <View style={GlobalStyles.flexRowEnd}>
                                    <TextInput
                                        placeholder={'请填写已赎回投资本金'}
                                        value={this.state.redemption_principal}
                                        onChangeText={(text) => {
                                            this.setState({
                                                redemption_principal: text
                                            })
                                        }}
                                        style={[GlobalStyles.cellInput, GlobalStyles.isIOS ? null : styles.inputAndroid, {textAlign: 'right', color: '#bbbbc1', fontSize: 14, width: 210, }]} 
                                        underlineColorAndroid={'transparent'}
                                        keyboardType="numeric"
                                    >
                                    </TextInput>
                                    <Text style={GlobalStyles.userlistrightDanwei}>元</Text>
                                </View>                                    
                            </View>
                        </View> 
                        <View style={GlobalStyles.userlist}>
                            <View style={GlobalStyles.userlistright}>
                                <Text style={GlobalStyles.userlisttext}>已结算收益</Text>
                                <View style={GlobalStyles.flexRowEnd}>
                                    <TextInput
                                        placeholder={'请填写已结算收益'}
                                        value={this.state.profit}
                                        onChangeText={(text) => {
                                            this.setState({
                                                profit: text
                                            })
                                        }}
                                        style={[GlobalStyles.cellInput, GlobalStyles.isIOS ? null : styles.inputAndroid, {textAlign: 'right', color: '#bbbbc1', fontSize: 14, width: 210, }]} 
                                        underlineColorAndroid={'transparent'}
                                        keyboardType="numeric"
                                    >
                                    </TextInput>
                                    <Text style={GlobalStyles.userlistrightDanwei}>元</Text>
                                </View>                                    
                            </View>
                        </View> 
                        <View style={GlobalStyles.userlist}>
                            <View style={GlobalStyles.userlistright}>
                                <Text style={GlobalStyles.userlisttext}>权益总额</Text>
                                <View style={GlobalStyles.flexRowEnd}>
                                    <TextInput
                                        placeholder={'请填写权益总额'}
                                        value={this.state.profit_all}
                                        onChangeText={(text) => {
                                            this.setState({
                                                profit_all: text
                                            })
                                        }}
                                        style={[GlobalStyles.cellInput, GlobalStyles.isIOS ? null : styles.inputAndroid, {textAlign: 'right', color: '#bbbbc1', fontSize: 14, width: 210, }]} 
                                        underlineColorAndroid={'transparent'}
                                        keyboardType="numeric"
                                    >
                                    </TextInput>
                                    <Text style={GlobalStyles.userlistrightDanwei}>元</Text>
                                </View>                                    
                            </View>
                        </View> 
                    </View> 
                    <TouchableOpacity onPress={()=>this.Subziliao()} style={[GlobalStyles.submit, {marginBottom: 15}]}>
                        <View style={GlobalStyles.btn}>
                            <Text style={GlobalStyles.btna}>提交</Text>   
                        </View>
                    </TouchableOpacity>
                </ScrollView> 
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: GlobalStyles.bgColor,
    },
});
